const {mainData} = require('./data/main')
const {first} = require('./data/init')
const Koa = require('koa')
const router = require('koa-router')();
const app = new Koa()
const cors = require('koa2-cors')


//const Mock = require('mockjs')
//const Random = Mock.Random
const web = 'http://rrq8dq1o2.hn-bkt.clouddn.com/';

app.use(cors({
    origin: function(ctx) { //设置允许来自指定域名请求
        // if (ctx.url === '/test') {
        return '*'; // 允许来自所有域名请求
        // }
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
}))

router.get('/design/init',async(ctx)=>{
    ctx.body={
        success:true,
        data:first
    }
})

router.get('/design/main',async(ctx)=>{
    const {color,config,tyre,interior} = ctx.request.query
    //console.log(color,config,tyre,interior);
    const colorData = mainData[color]
    const configData  = colorData[config]
    const tyreData = configData[tyre]
    const newData = {
        text1:colorData.text1,
        text2:colorData.text2,
        imgCar:tyreData,
        imgTyre:configData.tyreaAll,
        interior:mainData[interior]
    }

    ctx.body={
        success:true,
        data:newData
    }
})

router.get('/home/swiper',async(ctx)=>{
    //console.log(11);
   ctx.body={
       success:true,
    //    data:{
    //        Module3:'${web}swiper/Model-3.jpg',
    //        ModuleS:'${web}swiper/Model-S.jpg',
    //        ModuleX:'${web}swiper/Model-X.jpg',
    //        ModuleY:'${web}swiper/Model-Y.jpg'
    //    }
    data:[
        `${web}swiper/Model-3.jpg`,
        `${web}swiper/Model-Y.jpg`,
        `${web}swiper/Model-S.jpg`,
        `${web}swiper/Model-X.jpg`


    ]
   }
})

app
    .use(router.routes())
    .use(router.allowedMethods())


app.listen(8080, () => {
    //console.log(mainData);
    console.log('server is running in port 8080')
})